<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_setting_class extends AWS_CONTROLLER
{

	function get_permission_action()
	{
	
	}

	public function setup()
	{
		admin_session_class::check_admin_login($this->get_permission_action());
		$this->crumb("系统设置", "admin/setting/");
	}

	public function index_action()
	{
		$this->setting_action();
	}

	public function setting_action()
	{
		if (intval($_GET['group_id']) == 0)
		{
			$setting = $this->model('setting')->get_vars_by_group(0, true);
		}
		else
		{
			$setting = $this->model('setting')->get_vars_by_group($_GET['group_id'], true);
		}
		
		if (empty($setting))
		{
			H::redirect_msg('找不到对应的栏目');
		}
		
		TPL::assign('group_id', $_GET['group_id']);
		TPL::assign('setting', $setting);
		TPL::output("admin/global/setting");
	}

	/**
	 * 保存设置
	 * Enter description here ...
	 */
	public function sys_save_ajax_action()
	{
		define('IN_AJAX', TRUE);
		
		if ($_POST['upload_dir'] && preg_match('/(.*)\/$/i', $_POST['upload_dir']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "上传文件存放绝对路径结尾不带/"));
		}
		
		if ($_POST['upload_url'] && preg_match('/(.*)\/$/i', $_POST['upload_url']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "上传目录外部访问URL地址结尾不带/"));
		}
		
		if ($_POST['request_route'])
		{
			if ($request_routes = explode("\n", $_POST['request_route']))
			{
				foreach ($request_routes as $key => $val)
				{
					$val = trim($val);
					
					if (empty($val))
					{
						continue;
					}
					
					foreach (explode('===', $val) as $m => $n)
					{
						if (substr($n, 0, 1) != '/')
						{
							H::ajax_json_output(AWS_APP::RSM(null, "-1", "URL自定义路由规则错误：URL必须以/开头"));
						}
					}
				}
			}
		}

		$vars = $this->model('setting')->check_vars($_POST);	//过滤参数
		
		$retval = $this->model('setting')->set_vars($vars);		//保存参数到数据库
		
		if ($retval)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "1", "系统设置修改成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "系统设置修改失败"));
		}
	}

	public function test_email_setting()
	{
		define('IN_AJAX', TRUE);
		
		$smtp_config = array(
			'smtp_server' => $_POST['smtp_server'], 
			'smtp_ssl' => $_POST['smtp_ssl'], 
			'smtp_port' => $_POST['smtp_port'], 
			'smtp_username' => $_POST['smtp_username'], 
			'smtp_password' => $_POST['smtp_password']
		);
		
		$core_mail = new core_mail();
		
		$core_mail->debug = true;
		
		if (! $core_mail->connect(false, $_POST['email_type'], $smtp_config))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "测试邮件发送失败，请检查邮箱服务器设置"));
		}
		
		if ($core_mail->send_mail($_POST['from_email'], get_setting('site_name'), $_POST['test_email'], $_POST['test_email'], get_setting('site_name') . " - 邮箱服务器配置测试", '这是一封测试邮件，收到邮件表明网站邮箱服务器配置成功'))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "1", "测试邮件已发送，请查收邮件以确定配置是否正确"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(null, "1", "测试邮件发送失败，请检查邮箱服务器设置"));
		}
	}
}